// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: 0,
    windowWidth: getApp().globalData.windowWidth,
    windowHeight: getApp().globalData.windowHeight,
    signup: 0,
    hint: "",
    id: "",
    password: ""
  },

  input_id: function (e) {
    this.setData({
      id: e.detail.value
    })
  },

  input_password: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      role: options.type
    })

    var hint = ""
    switch (options.type) {
      case "1":
        hint = "请输入您的学号"
        break;
      case "2":
        hint = "请输入您的教工号"
        break;
      case "3":
        hint = "请输入您的管理员ID号"
        break;
      case "4":
        hint = "请输入您的用户名"
        break;
      default:
    }
    that.setData({
      hint: hint
    })
  },

  login: function () {
    var that = this
    //登录
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: getApp().globalData.my_server + "/login/check",
            data: {
              kind: that.data.role,
              id: that.data.id,
              password: that.data.password,
              code: res.code,
              userInfo: getApp().globalData.userInfo
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              // console.log(res)
              
              if (res.data.error_no == 1) {
                getApp().globalData.kind = that.data.role 
                if (that.data.role == "3"){
                  wx.reLaunch({
                    //     url: "../applications/applications?type=" + that.data.role,
                    url: "../exam_admin/exam_admin",
                  })
                } else if (that.data.role == "2"){
                  wx.reLaunch({
                    //     url: "../applications/applications?type=" + that.data.role,
                    url: "../exam/exam",
                  })
                }
               
              } else if (res.data.error_no == 2) {
                wx.showModal({
                  title: '哎呀～',
                  content: res.data.error_msg,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '哎呀～',
                content: '登录失败',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function (res) {

            }
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });

  },

  signup: function () {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})